feat: add username field to signup validation schema - #24
Conversation
|
@svilupp0 is attempting to deploy a commit to the Yash Pouranik's projects Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughAdded a required username field (3–30 chars) to signup input validation and updated the signup controller to parse and include Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
backend/controllers/userAuth.controller.js (1)
16-19: Clarify the role of username in your application design.Username uniqueness is not enforced—the code accepts and stores usernames but neither checks for duplicates nor uses them for authentication (login uses email only). If usernames should be unique identifiers, add a uniqueness check in the signup handler and consider adding a database index. If usernames are meant as display names only, document this design intent to avoid future confusion.
🤖 Fix all issues with AI agents
In `@backend/controllers/userAuth.controller.js`:
- Line 4: The controller references z.ZodError in the login and signup handlers
but never imports z, causing a ReferenceError on validation failures; add the
Zod import (e.g. const { z } = require('zod');) near the top of
userAuth.controller.js alongside the existing import of loginSchema and
signupSchema so that the z.ZodError checks in the functions (where z.ZodError is
used) work and validation errors return the intended 400 responses.
🧹 Nitpick comments (2)
backend/utils/input.validation.js (2)
14-16: Consider adding format validation for username.The username field only validates length but allows any characters including spaces, special characters, or emojis. Depending on your requirements, you may want to add a regex constraint for alphanumeric characters and common symbols (e.g., underscores, hyphens).
Additionally, consider adding
.trim()before validation to prevent whitespace-only or padded usernames from passing validation.💡 Optional enhancement
username: z.string() + .trim() .min(3, { message: "Username must be at least 3 characters." }) - .max(30, { message: "Username is too long." }), + .max(30, { message: "Username is too long." }) + .regex(/^[a-zA-Z0-9_-]+$/, { message: "Username can only contain letters, numbers, underscores, and hyphens." }),
18-25: Email and password validation is duplicated from loginSchema.The email and password validation logic is repeated between
loginSchema(lines 4-10) andsignupSchema. Consider extracting reusable field schemas to reduce duplication and ensure consistency.♻️ Optional refactor to reduce duplication
// Reusable field schemas const emailField = z.string() .min(1, { message: "Email is required." }) .email({ message: "Invalid email format." }) .max(100, { message: "Email is too long." }); const passwordField = z.string() .min(6, { message: "Password must be at least 6 characters." }) .max(100, { message: "Password is too long." }); module.exports.loginSchema = z.object({ email: emailField, password: passwordField }); module.exports.signupSchema = z.object({ username: z.string() .min(3, { message: "Username must be at least 3 characters." }) .max(30, { message: "Username is too long." }), email: emailField, password: passwordField });
|
@svilupp0 Fix Issues sugggested by Coderabbit |
yash-pouranik
left a comment
There was a problem hiding this comment.
Thankx for PR @svilupp0 , feel free to add some big feature or fixing any bug, it will strengthen ur learning. All the best. ❤️
Closes #14
Changes
signupSchematobackend/utils/input.validation.jswith username validation (min 3 characters)userAuth.controller.jsto usesignupSchemaand save username during signupTesting
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.